<template>
  <div>
    <el-dialog
      :model-value="open"
      :title="title"
      :before-close="handleClose"
    >
      <el-form
        ref="setFormRef"
        :model="setForm"
        :rules="rules"
        label-width="120px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="城市" prop="cityName" :label-position="labelPosition">
              <el-input
                v-model="setForm.cityName"
                placeholder="请输入城市"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机场中文名" prop="airportName" :label-position="labelPosition">
              <el-input
                v-model="setForm.airportName"
                placeholder="请输入机场中文名"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="机场英文名" prop="airportNameEn" :label-position="labelPosition">
              <el-input
                v-model="setForm.airportNameEn"
                placeholder="请输入机场英文名"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机场三字码" prop="airportThreeCode" :label-position="labelPosition">
              <el-input
                v-model="setForm.airportThreeCode"
                placeholder="请输入机场三字码"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="机场四字码" prop="airportQuadrupleCode" :label-position="labelPosition">
              <el-input
                v-model="setForm.airportQuadrupleCode"
                placeholder="请输入机场四字码"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div>
          <el-button type="primary" @click="submit">确定</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, getCurrentInstance, defineExpose } from 'vue'
import { ElMessage } from 'element-plus'
import { addAirport, getAirportDetail, editAirport } from '@/api/basicConfig/airportMaintanance'

const porps = defineProps({
  title: {
    type: String,
    default: '',
  },
  open: {
    type: Boolean,
  },
})
const 
  setForm = ref({
    id: undefined,
    cityName: undefined,
    airportName: undefined,
    airportNameEn: undefined,
    airportThreeCode: undefined,
    airportQuadrupleCode: undefined,
  }),
  rules = ref({
    cityName: [{ required: true, message: '必填', trigger: 'blur' }],
    airportName: [{ required: true, message: '必填', trigger: 'blur' }],
    airportNameEn: [{ required: true, message: '必填', trigger: 'blur' }],
    airportThreeCode: [{ required: true, message: '必填', trigger: 'blur' }],
    airportQuadrupleCode: [{ required: true, message: '必填', trigger: 'blur' }],
  }),
  labelPosition = ref('right'),
  setFormRef = ref(null),
  {
    proxy,
  }:{
   proxy:any
 } = getCurrentInstance()

const emit = defineEmits(['changeList', 'update:open'])

const reset = () => {
  setForm.value = {
    id: undefined,
    cityName: undefined,
    airportName: undefined,
    airportNameEn: undefined,
    airportThreeCode: undefined,
    airportQuadrupleCode: undefined,
  }
  proxy.$resetForm(setFormRef.value)
}
const submit = () => {
  proxy.$validateForm(setFormRef.value, async () => {
    let res = null
    if (setForm.value.id != undefined) {
      res = await editAirport(setForm.value)
    } else {
      res = await addAirport(setForm.value)
    }
    if (res.code == 200) {
      ElMessage.success(`${ porps.title }成功`)
      emit('update:open', false)
      reset()
      emit('changeList')
      reset()
    } else {
      ElMessage.error(`${ porps.title }失败`)
    }
  })
}
const cancel = () => {
  reset()
  emit('update:open', false)
}
const getDetail = (id) => {
  getAirportDetail(id).then((res) => {
    if (res.code == 200) {
      setForm.value = res.data
    }
  })
}
const handleClose = () => {
  reset()
  emit('update:open', false)
}
defineExpose({
  getDetail,
})
</script>
